﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using Yoozoo.Managers;

namespace Gameplay.PVE.Survivor
{
    /// <summary>
    /// 管理角色被控制
    /// </summary>
    public class ControlActionComponent: ComponentBase
    {
        private List<ControlAction> actionList = new List<ControlAction>();
        private ControlAction currentPlayingAction;
        protected override void OnInitialized()
        {
            
        }

        protected override void OnUpdate()
        {
            currentPlayingAction?.Update();
        }

        public bool IsInControl()
        {
            return currentPlayingAction != null;
        }

        public float AddAction(ControlElement element)
        {
            if (unit.isDead)
            {
                return 0;
            }
            ControlAction action = null;
            var type = (EBattleElementSubType)element.config.sub_type;
            if (type == EBattleElementSubType.KnockFly)
            {
                action = ClassManager.Get<KnockFlyAction>();
            }
            else if (type == EBattleElementSubType.Freeze)
            {
                action = ClassManager.Get<FreezeAction>();
            }

            if (action != null)
            {
                action.Initialize(unit,element);
                //按照优先级加入
                var priority = action.GetPriority();
                var insertIndex = 0;
                for (int i = 0; i < actionList.Count; i++)
                {
                    if (actionList[i].GetPriority() <= priority)
                    {
                        insertIndex = i;
                        break;
                    }
                }
                actionList.Insert(insertIndex, action);
                CheckHeadChanged();
                return action.GetDuration();
            }

            return 0;
        }

        public void RemoveAction(ControlElement element)
        {
            if (unit.isDead)
            {
                return;
            }
            for(int i = 0; i < actionList.Count; i++)
            {
                if (actionList[i].controlElement == element)
                {
                    actionList[i].End();
                    if (currentPlayingAction == actionList[i])
                    {
                        currentPlayingAction = null;
                    }
                    ClassManager.Free(actionList[i]);
                    actionList.RemoveAt(i);
                    break;
                }
            }
            CheckHeadChanged();
        }

        private void CheckHeadChanged()
        {
            if (actionList.Count == 0)
            {
                if (currentPlayingAction != null)
                {
                    currentPlayingAction.Pause();
                    currentPlayingAction = null;
                }
                return;
            }
            var head = actionList[0];
            if (head != currentPlayingAction && currentPlayingAction != null)
            {
                currentPlayingAction.Pause();
            }
            head.Start();
            currentPlayingAction = head;
        }

        protected override void OnDead()
        {
            base.OnDead();
            actionList.Clear();
            if (currentPlayingAction != null)
            {
                currentPlayingAction.Pause();
                currentPlayingAction = null;
            }
        }
    }
}
